var util = require("../../../utils/util.js");
// pages/myrecipients/myrecipients.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    my_receiver_list: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options + "分享带参数");
    console.log(options);
    // if (options.isShare == 1) {
    //   wx.navigateTo({
    //     url: 'page/my/orderDetail/orderDetail?order_id=' + options.order_id,
    //   })
    // }
    wx.setStorageSync("order_id", options.order_id);
    this.setData({
      order_id: options.order_id,
      order_bn: options.order_bn
    })
    var that = this;
    wx.setNavigationBarTitle({
      title: '我的收件人',
    })
    // 获取本地数据-用户信息
    // wx.getStorage({
    //   key: 'orderInfoNow',
    //   // 能获取到则显示用户信息，保存信息到页面
    //   success: (json) => {
    //     console.log(json);
    //     this.setData({
    //       orderInfoNow: json.data.orderInfoNow
    //     })
    //   }
    // });
    // 获取本地数据-用户信息
    wx.getStorage({
      key: 'userInfo',
      // 能获取到则显示用户信息，并保持登录状态，不能就什么也不做
      success: (json) => {
        console.log(json);
        // 请求获取进行中的订单个数
        const requestTask = wx.request({
          url: 'https://boxapi.supergk.com/server/user/my_receiver_list',
          data: {
            uid: json.data.userInfo.uid,
            // uid: '1',
            // page: '1',
            // psize: '10'
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res);
            // 存储我的收件人列表
            that.setData({
              my_receiver_list: res.data.data.my_receiver_list,
              lock: true
            })
          }
        })
      }
    });


  },

  setMyRecipient: function (e) {
    var that = this;
    const dataset = e.currentTarget.dataset;
    var _recipientId = dataset.id;
    var _order_id = this.data.order_id;
    var _uid = wx.getStorageSync("userInfo").userInfo.uid;
    var orderInfoNow = this.data.orderInfoNow;
    // orderInfoNow.recipientId = _recipientId;
    that.setData({
      orderInfoNow: orderInfoNow,
    })
    // 设置收件人
    const requestSetMyRecipient = wx.request({
      url: "https://boxapi.supergk.com/server/order/add_receiver",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: util.json2Form({ order_id: _order_id, uid: _uid, choose_uid: _recipientId }),
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          console.log(res.data.msg);
          wx.navigateTo({
            url: '../../index/index'
            // url: '../../sendBox/sendBox?order_id=' + _order_id
          })

        } else {
          console.log(res.data.msg)
        }
      }
    })
    // 存储用户信息到本地
    wx.setStorage({
      key: 'orderInfoNow',
      data: {
        orderInfoNow: orderInfoNow,
      },
      success: function (res) {
        console.log("存储成功")
      },
    })
  },

  // 店家邀请微信好友
  inviteMyWechat: function () {
    wx.showModal({
      title: '邀请微信好友',
      content: '点击右上角分享给微信好友即可',
      showCancel: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  //分享
  onShareAppMessage: function () {
    var that = this;
    var order_id = that.data.order_id;
    // var order_id = wx.getStorageSync("order_id");
    var path = '/pages/my/ordeDetail/ordeDetail?isShare=1&order_id=' + order_id;
    return {
      title: "我发送了一个智能箱子给你",
      path: path
    }
  }

})